/**
 * Created by aa on 2016/10/14.
 */
'use strict';
var utils = require('../common/utils');
var cac = utils.cac;

//获取登录验证码链接
const RECEIVE_MSG_IMGURL = 'RECEIVE_MSG_IMGURL';

let receiveMsgImgUrl;

//对外暴露变量
exports.RECEIVE_MSG_IMGURL = RECEIVE_MSG_IMGURL;


exports.receiveMsgImgUrl = receiveMsgImgUrl = cac(RECEIVE_MSG_IMGURL, 'msgImgUrl');


exports.getMsgCodeImg = (finishCallBack)=> {
    return (dispatch, getState) => {
        //此处添加随机数的版本号，坑爹的ie同一个请求会缓存结果，导致验证码无法更新
        var random = Math.random();
        reqwest({
            url: '/api/getMsgCodeImg?version=' + random,
            method: 'get',
            contentType: 'application/json'
        })
            .then(function (data) {
                setTimeout(()=> {
                    dispatch(receiveMsgImgUrl(data.result));
                    finishCallBack();
                }, 0);
            })
            .fail(function (err, msg) {
                alert('获取数据出错');
            });
    }
};

exports.changePassWord = (oldPwd, pwd, verifyCode, finishCallBack,errorCallBack)=> {
    return (dispatch, getState) => {
        reqwest({
            url: '/api/changePassWord',
            method: 'put',
            data: JSON.stringify({
                oldPwd: oldPwd,
                pwd: pwd,
                verifyCode: verifyCode
            }),
            contentType: 'application/json'
        })
            .then(function (data) {
                finishCallBack(data);
            })
            .fail(function (err, msg) {
                errorCallBack('获取数据出错');
            });
    }
};